#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

#include "Box2D/Box2D.h"

#include "SimpleAudioEngine.h"

#include "GroundBox.h"

#include "ICC.h"

//#include "GLES-Render.h"

#include "SpriteLayer.h"

//#include "ContactListener.h"

#include "Prop.h"

#include "SourceNum.h"


class Jump{
private:
	bool canDestroy;
	bool isJumpping;
	bool isPassed;
	float hightvalue;
	float starty;
	b2Vec2 jumpForce;
public:
	Jump(){
		isJumpping = false;
		isPassed = false;
		hightvalue = JUMP_HIGHT;
		starty = 0;
		canDestroy = false;
	}
	void setJumpState(bool is){
		isJumpping = is;
	}
	bool getJumpState(){
		return isJumpping;
	}
	bool couldApplyForce(float y){
		if (!isPassed && (hightvalue > (y-starty)))
		{
			cocos2d::CCLog("%f",y-starty);
			return true;
		}
		else{
			//canDestroy = true;
			isPassed = true;
			return false;
		}
	}
	void jumpStart(float y){
		starty = y;
		isPassed = false;
		//canDestroy = false;
	}
	void setDestroy(bool is){
		canDestroy = is;
	}
	bool getDestroy(){
		return canDestroy;
	}
	void setForce(const b2Vec2 &v){
		jumpForce = v;
	}
	b2Vec2 getForce(){
		return jumpForce;
	}

};






class Box2dLayer : public cocos2d::CCLayer,public b2ContactListener
{
private:
	b2World* world;
	//GLESDebugDraw* debugDraw;
	ICC *icc;
	GroundBox *gbox;

	float length[GROUNDS];
	float currentLength;
	float required_speed;
	float speedtime;

	b2Body *body[GROUNDS];
	b2Body *sprite;
	b2Body *lowerSprite;
	b2Body *leftSprite;
	b2Body *jumpSprite;
	b2Body *prop[5];

	//b2Body *clonesprite,*clonelowerSprite,*cloneleftSprite;
	b2Vec2 preVel[NUM_PREV_VELS];
	int nextVel;

	Jump *jumpData;

public:
	Box2dLayer();
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    //virtual bool init();  

    // there's no 'id' in cpp, so we recommand to return the exactly class pointer
    static cocos2d::CCScene* scene();
    
    // a selector callback
    virtual void menuCloseCallback(CCObject* pSender);

    // implement the "static node()" method manually


	virtual void draw();
	virtual void update(float dt);
	b2Vec2 getAverageVel(const b2Vec2 &bv);
	void initData();
	void createWorld();
	void createGround();
	void createBody();
	void createJoint();

	//void createTempBody();
	//void createTempJoint();
	//void tempBodyMove();
	//void destroyTempBody();

	void createJumpRole();
	b2Vec2 getJumpV(const b2Vec2 &v);
	void destroyJumpRole();

	void addGround(int i);

	void backgroundMove();
	void spriteMove();
	void moveOver();
	void jumpRoleMove();

	void createProp();
	void MovePro();

	void beforeWorldStart();

	bool isRoleCanMove();

	bool isRequireSpeed(float vx,float vy){
		return (required_speed*required_speed<(vx*vx+vy*vy)?true:false);
	};
	void setDoubleSpeed(){
		required_speed = 2 * REQUIRED_SPEED;
		speedtime = 5*60;
	}
	void setNormalSpeed();
	void controlSpeed();
	void stopSprite(b2Body *body);

   // LAYER_NODE_FUNC(Box2dLayer);
	void addGrounds();


	void onEnter();
	bool ccTouchBegan(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);
	//void ccTouchMoved(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);
	void ccTouchEnded(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);


	void BeginContact(b2Contact* contact);


};

#endif  // __HELLOWORLD_SCENE_H__